<template>
  <view>
    <!-- 选择收货的地址盒子 -->
    <view class="address-choose-box" v-if="JSON.stringify(address) === '{}'">
      <button type="primary" size="mini" class="btnChooseAddress" @click="chooseAddress">请选择收货地址+</button>
    </view>

    <!-- 渲染收货信息的盒子 -->
    <view class="address-info-box" v-else @click="chooseAddress">
      <!-- 收货人 和 电话 -->
      <view class="row1">
        <view class="row1-left">
          <view class="username">收货人：<text>{{address.userName}}</text></view>
        </view>

        <view class="row1-right">
          <view class="phone">电话：<text>{{address.telNumber}}</text></view>
          <uni-icons type="arrowright" size="16"></uni-icons>
        </view>
      </view>

      <!-- 收货地址 -->
      <view class="row2">
        <view class="row2-left">收货地址：</view>
        <view class="row2-right">{{addstr}}</view>
      </view>
    </view>

    <!-- 底部边线样式 -->
    <image src="/static/cart_border@2x.png" class="address-border"></image>
  </view>


</template>
<script>
  import {
    mapState,
    mapMutations,
    mapGetters
  } from 'vuex'

  export default {
    computed: {
      // 2.2 把 m_user 模块中的 address 对象映射当前组件中使用，代替 data 中 address 对象
      ...mapState('m_user', ['address']),
      ...mapGetters('m_user', ['addstr']),

    },
    data() {
      return {
        // 2.1 注释掉下面的 address 对象，使用 2.2 中的代码替代之
        // address: []
      }
    },

    methods: {
      // 3.1 把 m_user 模块中的 updateAddress 函数映射到当前组件
      ...mapMutations('m_user', ['updateAddress']),
      async chooseAddress() {
        const [err, succ] = await uni.chooseAddress().catch(err => err)
        console.log(succ)
        console.log(err)
        

         if (succ && succ.errMsg === 'chooseAddress:ok') {
            // 更新 vuex 中的收货地址
            this.updateAddress(succ)
          }

        if (err && (err.errMsg === 'chooseAddress:fail authorize no response' || err.errMsg === 'chooseAddress:fail auth deny' || err.errMsg === 'hooseAddress:fail the api need to be declared in …e requiredPrivateInfos field in app.json/ext.json')) {
          // 需要重新授权
          this.reAuth()
        }

      },

      // 重新获取授权
      async reAuth() {
        // 1.提示用户对地址进行授权
        const [err2, confirmResult] = await uni.showModal({
          content: '检测到你没有打开地址权限，是否区设置打开？',
          confirmText: '确认',
          cancelText: '取消'
        })

        console.log(confirmResult)
        // 2.如果单框异常,直接退出
        if (err2) return
        // 3.用户点击取消按钮,提示你取消地址授权
        if (confirmResult.cancel) return uni.$showMsg('你取消地址授权')
        // 4.用户点击确认按钮,调用uni.openSetting()方法进入授权页面
        if (confirmResult.confirm) return uni.openSetting({
          success: (settingResult) => {
            console.log(settingResult)
            if (settingResult.authSetting['scope.address']) return uni.$showMsg('授权成功！请选择地址')
            if (!settingResult.authSetting['scope.address']) return uni.$showMsg('你取消地址授权！')

          }
        })

      }
    }
  }
</script>
<style lang="scss">
  .address-border {
    display: block;
    width: 100%;
    height: 5px;
    margin-top: 5px;
  }

  .address-choose-box {
    height: 90px;
    display: flex;
    align-items: center;
    justify-content: center;


  }

  .address-info-box {
    font-size: 18px;
    height: 90px;
    display: flex;
    justify-content: center;
    flex-direction: column;
    padding: 10px 5px;

    .row1 {
      display: flex;
      justify-content: space-between;


      .row1-right {
        display: flex;
        justify-content: space-between;


      }
    }

    .row2 {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-top: 10px;

      .row2-left {
        white-space: nowrap;
      }
    }
  }
</style>
